package com.homegoods.orderinfodetail.presenter;

import android.databinding.ObservableBoolean;
import android.util.Log;
import android.view.View;

import com.alibaba.android.arouter.launcher.ARouter;
import com.alibaba.fastjson.JSON;
import com.homegoods.area.model.SubModelList;
import com.homegoods.orderinfodetail.OrderInfoApiUtils;
import com.homegoods.orderinfodetail.bean.AddressListModel;
import com.homegoods.orderinfodetail.event.EditAddressEvent;
import com.homegoods.orderinfodetail.model.AddressModel;
import com.homegoods.aroutercomment.ARouterPath;
import com.homegoods.resource.HomeGoodsApp;
import com.kedll.library.adapter.MultiTypeAdapter;
import com.kedll.library.mvp.BasePresenter;
import com.kedll.library.mvp.BaseView;
import com.kedll.library.util.RxBus;

import java.util.List;
import java.util.Random;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * @author QiQi
 */
public class AddressManagerPresenter extends BasePresenter<BaseView> {

    public final ObservableBoolean haveAddress = new ObservableBoolean();
    private MultiTypeAdapter adapter = new MultiTypeAdapter();

    public AddressManagerPresenter(BaseView view) {
        super(view);

        //添加地址成功，返回新添加的地址，并在我的收货地址界面上显示
        addSubscription(RxBus.getDefault()
                .toFlowable(AddressModel.class)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(o -> adapter.addItem(0, o.createItem(adapter))));

        addSubscription(RxBus.getDefault()
                .toFlowable(EditAddressEvent.class)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(editAddressEvent -> {
                    haveAddress.set(true);
                    boolean isEdit = editAddressEvent.isEdit;
                    AddressListModel model = editAddressEvent.addressModel;
                    if (isEdit) {
                        //编辑模式，刷新整个列表，为了刷新默认地址的设置
                        getAddresses();
                    } else {
                        adapter.addItem(model.createItem(adapter));
                        adapter.notifyDataSetChanged();
                    }
                }));


    }

    public MultiTypeAdapter getAdapter() {
        return adapter;
    }

    /**
     * 获取收货地址
     */
    public void getAddresses() {
        OrderInfoApiUtils.getCityList(1, 10, new Consumer<SubModelList>() {
            @Override
            public void accept(SubModelList subModelList) throws Exception {
                Log.d("zhengshibo", "accept: subModelList == " + JSON.toJSONString(subModelList));
                List<AddressListModel> list = JSON.parseArray(subModelList.getItems(), AddressListModel.class);
//                String s = JSON.toJSONString(subModelList);
//                Log.d("zhengshibo", "accept: s == " + s);
//                List<AddressListModel> list = JSON.parseArray(s, AddressListModel.class);
                if (list != null && list.size() > 0) {
                    haveAddress.set(true);
                }
                adapter.clear();
                for (AddressListModel model : list) {
                    adapter.addItem(model.createItem(adapter));
                }
                adapter.notifyDataSetChanged();
            }
        }, throwable -> {
        });
//        }


    }

    public void addNewAddress(View view) {
        ARouter.getInstance()
                .build(ARouterPath.PATH_ADD_NEW_ADDRESS)
                .navigation();
    }
}